//
//  BCUpStuffVC.m
//  BangcaiSale
//
//  Created by lsy on 16/7/7.
//  Copyright © 2016年 Bangcai. All rights reserved.
//

#import "BCUpStuffVC.h"
#import "WKPageView.h"
#import "BCStuffFootView.h"
#import "BCSearchResult.h"
@class WKPageView;

//#import "BCOrderViewController.h"
//#import "BCStuffInfoViewController.h"

@interface BCUpStuffVC ()<WKPageViewDataSource,WKPageViewDelegate, clickDelegate>
@property (nonatomic, strong) NSArray *items;
@property (nonatomic, strong) WKPageView *pageView;


@property (nonatomic, strong)NSArray *dataArr;                      //数据数组
@property (nonatomic, strong)NSMutableArray *amoutArr;              //是否被选中数组
/** 搜索框绿色渐变背景视图 */
@property (nonatomic, strong)BCStuffFootView *footView;
@property (nonatomic, strong) UIView *greenView;
/** 搜索view */
@property (nonatomic, strong) UIView *searchView;
@property (nonatomic, strong) UIButton *backButton;

@end

@implementation BCUpStuffVC
- (instancetype)initWithIndex:(NSIndexPath *)index
{
    self = [super init];
    if (self) {
        self.ClickIndex = index;
        [self getValue];
        [self getView];
    }
    return self;
}

-(void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self p_initTopView];
    [self p_initSearchView];
}

-(void)viewWillAppear:(BOOL)animated {
    self.navigationController.navigationBar.hidden = YES;
    if (!self.rdv_tabBarController.tabBarHidden) {
        [self.rdv_tabBarController setTabBarHidden:YES];
    }
  }

-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    if (self.rdv_tabBarController.tabBarHidden) {
        [self.rdv_tabBarController setTabBarHidden:NO];
    }
}
- (void)getValue
{
    self.items = [NSArray arrayWithObjects:@"禽蛋肉品",@"蔬菜水果",@"水产冻品",@"调料干货",@"米面粮油",@"酒水饮料",@"熟食面点",@"厨房用品", nil];
    NSArray *arr0 = @[@"全部",
                      @"猪肉",
                      @"牛肉",
                      @"羊肉",
                      @"禽类",
                      @"猪肉冻",
                      @"禽类冻",
                      @"牛羊肉冻",
                      @"蛋类",
                      @"活禽",
                      @"调理肉类"];
    NSArray *arr1 = @[@"全部",
                      @"叶菜类",
                      @"茄果类",
                      @"根茎类",
                      @"豆类",
                      @"特菜",
                      @"菌类",
                      @"葱姜蒜",
                      @"水果"];
    NSArray *arr2 = @[@"全部",
                      @"水产类",
                      @"水发类",
                      @"冷冻水产",
                      @"火锅丸类"];
    NSArray *arr3 = @[@"全部",
                      @"酱油醋",
                      @"腌菜罐头",
                      @"香辛料",
                      @"调味油",
                      @"调味酱",
                      @"糖、盐",
                      @"南北干货",
                      @"调味料",
                      @"其他"];
    NSArray *arr4 = @[@"全部",
                      @"大米",
                      @"面粉面条",
                      @"杂粮",
                      @"食用油"];
    NSArray *arr5 = @[@"全部",
                      @"乳品",
                      @"白酒红酒",
                      @"啤酒",
                      @"饮料",
                      @"饮用水"];
    NSArray *arr6 = @[@"全部",
                      @"主食",
                      @"熟食",
                      @"冷冻面点",
                      @"豆制品"];
    NSArray *arr7 = @[@"全部",
                      @"清洁用品",
                      @"厨房用具",
                      @"一次性消耗品"];
    self.dataArr = [NSArray arrayWithObjects:arr0, arr1, arr2, arr3, arr4, arr5, arr6, arr7, nil];
    
    self.amoutArr = [NSMutableArray array];
    for (NSInteger i = 0; i < 8; i ++) {
        NSMutableArray *arr = [NSMutableArray array];
        [self.amoutArr addObject:arr];
        
    };
    
}

- (void)getView
{
    
    self.edgesForExtendedLayout = UIRectEdgeNone;
    WKPageView *pageView = [[WKPageView alloc] initWithFrame:CGRectMake(0, 65.f, self.view.bounds.size.width, self.view.bounds.size.height-65.f)];
    pageView.dataSource = self;
    pageView.delegate = self;
    pageView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:pageView];
    self.pageView = pageView;
    self.footView = [[BCStuffFootView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 50, SCREEN_WIDTH, 50)];
    self.footView.logoImg = [UIImage imageNamed:@"购物车1"];
    [self.view addSubview:self.footView];
    [self.footView.coomit addTarget:self action:@selector(commit:) forControlEvents:UIControlEventTouchUpInside];
    
}

- (void)commit:(UIButton *)but
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark 协议事件
- (void)goSecond{
    NSLog(@"click Second");
}

- (void)addAmount{
    
    self.footView.amount +=1;
}

- (void)removeAmount
{
    self.footView.amount -=1;

}

- (void)getAmount:(NSMutableArray *)arr index:(NSInteger)index
{
    self.amoutArr[index] = arr;
//    [self.pageView reloadDataWith:index];
}

/**
 *  初始化顶部视图view
 */
- (void)p_initTopView {
    self.greenView.frame = CGRectMake(0, 0, self.view.frame.size.width , 65);
    self.greenView.backgroundColor = [UIColor colorWithRed:0.205  green:0.285  blue:0.368 alpha:1];
    self.greenView.alpha = 0.95f;
    [self.view addSubview:self.greenView];
}

/**
 *  初始化搜索框view
 */
- (void)p_initSearchView {
    
    UIImageView *backImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"BackArrow"]];;
    backImageView.frame = CGRectMake(17.5f, 30.f, 10.f, 20.f);
    [self.greenView addSubview:backImageView];
    
    self.backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.backButton.frame = CGRectMake(0, 0, 40, 40);
    self.backButton.center = backImageView.center;
    [self.backButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    [self.greenView addSubview:self.backButton];
    
    UIView *searchBar = [[UIView alloc] initWithFrame:CGRectMake(45.f, 0, self.greenView.frame.size.width-45.f-16.f, 29.f)];
    searchBar.center = CGPointMake(searchBar.center.x, backImageView.center.y);
    searchBar.backgroundColor = [UIColor colorWithRed:0.045  green:0.189  blue:0.334 alpha:1];
    searchBar.layer.cornerRadius = 8.f;
    [self.greenView addSubview:searchBar];
    
    UIImageView *searchIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"搜索"]];
    searchIconView.frame = CGRectMake(8.f, 8.f, 15.f, 15.f);
    [searchBar addSubview:searchIconView];
    
    UILabel *tipLable = [[UILabel alloc] initWithFrame:CGRectMake(searchIconView.frame.origin.x+searchIconView.frame.size.width+2.f, 0, searchBar.frame.size.width-(searchIconView.frame.origin.x*2)-searchIconView.frame.size.width, searchBar.frame.size.height)];
    tipLable.text = @"去搜索";
    tipLable.backgroundColor = [UIColor clearColor];
    tipLable.font = [UIFont boldSystemFontOfSize:15.f];
    tipLable.textAlignment = NSTextAlignmentLeft;
    tipLable.textColor = [UIColor whiteColor];
    [searchBar addSubview:tipLable];
    
    UIButton *searchButton = [UIButton buttonWithType:UIButtonTypeCustom];
    searchButton.frame = CGRectMake(searchBar.frame.origin.x, 0, searchBar.frame.size.width, self.greenView.frame.size.height);
    searchButton.backgroundColor = [UIColor clearColor];
    [searchButton addTarget:self action:@selector(p_startSearch) forControlEvents:UIControlEventTouchUpInside];
    [self.greenView addSubview:searchButton];
}

#pragma mark - page view datasource
// Menu的标题用NSArray封装，内为NSString
- (NSArray *)menuItemsForMenuViewInPageView:(WKPageView *)pageView{
    return self.items;
}

- (WKPageCell *)pageView:(WKPageView *)pageView cellForIndex:(NSInteger)index{
    static NSString *identifier = @"pageCell";
    WKPageCell *cell = [pageView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[WKPageCell alloc] initWithIdentifier:identifier];
    }
    cell.backgroundColor = [UIColor colorWithRed:0.949  green:0.949  blue:0.949 alpha:1];
    cell.secondArr = self.dataArr[index];
    cell.chooseNumArr = self.amoutArr[index];
    cell.delegate = self;
    cell.index = index;
    cell.reddotAnimationBlock = ^(CAAnimation *anim) {
        if ([[anim valueForKey:@"animationName"]isEqualToString:@"groupsAnimation"]) {
            
            CABasicAnimation *shakeAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
            shakeAnimation.duration = 0.25f;
            shakeAnimation.fromValue = [NSNumber numberWithFloat:0.9];
            shakeAnimation.toValue = [NSNumber numberWithFloat:1];
            shakeAnimation.autoreverses = YES;
        }
    };
    return cell;
}
// 若不实现，默认红黑，为保证字体颜色渐变，请尽量选用RGBA来创建UIColor
- (UIColor *)titleColorOfMenuItemInPageView:(WKPageView *)pageView withState:(WKMenuItemTitleColorState)state{
    if (state == WKMenuItemTitleSizeStateNormal) {
        return [UIColor colorWithRed:0.2  green:0.2  blue:0.2 alpha:1];
    }else if (state == WKMenuItemTitleSizeStateSelected)
    {
        return [UIColor colorWithRed:0  green:0.454  blue:0.999 alpha:1];
    }else
    {
        return [UIColor redColor];
    }
}

// 若不实现,默认为30
- (CGFloat)pageView:(WKPageView *)pageView heightForMenuView:(WKMenuView *)menuView{
    return 50;
}
// 若不实现，默认为15/18
- (CGFloat)titleSizeOfMenuItemInPageView:(WKPageView *)pageView withState:(WKMenuItemTitleSizeState)state{
    switch (state) {
        case WKMenuItemTitleSizeStateNormal:
            return 14;
            break;
        case WKMenuItemTitleSizeStateSelected:
            return 14;
        default:
            break;
    }
}
// 若不实现，默认为灰色
- (UIColor *)backgroundColorOfMenuViewInPageView:(WKPageView *)pageView{
    return [UIColor whiteColor];
}
// MenuView内部各个item的宽度，若标题过长可自行设置，默认为60
- (CGFloat)pageView:(WKPageView *)pageView widthForMenuItemAtIndex:(NSInteger)index{
    return 80;
}

#pragma mark - action
/**
 *  点击搜索框事件
 */
- (void)p_startSearch {
    //    NSLog(@"%s",__func__);
    BCSearchResult *searchResultViewController = [[BCSearchResult alloc] init];
    [self.navigationController pushViewController:searchResultViewController animated:YES];
}

-(void)backAction {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - Lazy
- (UIView *)greenView {
    if (_greenView == nil) {
        _greenView = [[UIView alloc] init];
    }
    return _greenView;
}


@end
